package com.hmall.trade.listener;




import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@RequiredArgsConstructor
@Component
@Slf4j
public class PayStatusListenner {

    private final IOrderService orderService;

    //监听支付成功消息
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue",durable = "true"),
            exchange = @Exchange(name = "pay.direct",type = ExchangeTypes.DIRECT),
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        //查询订单
        Order order = orderService.getById(orderId);
        //判断订单状态,是否为未支付
        if (order == null || order.getStatus() != 1){
            //如果不是未支付,不做处理
            return;
        }
        orderService.markOrderPaySuccess(orderId);
    }


}
